package com.li.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ndboo.reslibrary.R;

/**
 * Author: Li
 * Date: 2017/4/5
 * Description:
 */

public class ToastUtil {
    private static String oldMsg;
    protected static Toast toast = null;
    private static long oneTime = 0;
    private static long twoTime = 0;
    private static TextView textView;

    public static void showToast(Context context, int resId) {
        showToast(context, context.getString(resId));
    }

    public static void showToast(Context context, int resId, int gravity) {
        showToast(context, context.getString(resId), gravity, 0, 0);
    }

    public static void showToast(Context context, String s, int gravity) {
        showToast(context, s, gravity, 0, 0);
    }

    public static void showToast(Context context, int resId, int gravity, int offX, int offY) {
        showToast(context, context.getString(resId), gravity, offX, offY);
    }

    public static void showToast(Context context, String s) {
        if (toast == null) {
            toast=new Toast(context);
            View view= LayoutInflater.from(context).inflate(R.layout.layout_toast,null);
            textView = view.findViewById(R.id.txt_toast);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.setDuration(Toast.LENGTH_SHORT);
            textView.setText(s);
            toast.setView(view);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = s;
                textView.setText(s);
                toast.show();
            }
        }
        oneTime = twoTime;
    }
//    public static void showToast(Context context, String s) {
//        if (toast == null) {
////            toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
//            toast=new Toast(context);
//            View view= LayoutInflater.from(context).inflate(R.layout.layout_toast,null);
//            textView = view.findViewById(R.id.txt_toast);
//            toast.setGravity(Gravity.CENTER,0,0);
//            toast.setDuration(Toast.LENGTH_SHORT);
//            toast.setView(view);
//            toast.show();
//            oneTime = System.currentTimeMillis();
//        } else {
//            twoTime = System.currentTimeMillis();
//            if (s.equals(oldMsg)) {
//                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
//                    toast.show();
//                }
//            } else {
//                oldMsg = s;
//                toast.setText(s);
//                toast.show();
//            }
//        }
//        oneTime = twoTime;
//    }


    public static void showToast(Context context, String s, int gravity, int offX, int offY) {
        if (toast == null) {
            toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
            toast.setGravity(gravity, offX, offY);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = s;
                toast.setText(s);
                toast.show();
            }
        }
        oneTime = twoTime;
    }

}
